<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-04-01 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		$this->excute();
	}
	
	private function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'scenic_price':
				$this->scenicPrice();
				break;
			case 'scenic_list':
				$this->scenicList();
				break;
			case 'scenic_info':
				$this->scenicInfo();
				break;
			case 'add_scenic_price':
				$this->addScenicPrice();
				break;
			case 'delete_scenic_price':
				$this->deleteScenicPrice();
				break;
			case 'hostel_price':
				$this->hostelPrice();
				break;
			case 'hostel_room_price':
				$this->hostelRoomPrice();
				break;
			case 'hostel_list':
				$this->hoselList();
				break;
			case 'hostel_info':
				$this->hostelInfo();
				break;
			case 'add_hostel_price':
				$this->addHostelPrice();
				break;
			case 'delete_hostel_price':
				$this->deleteHostelPrice();
				break;
			default:
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function scenicPrice()
	{
		//DBFunction::getInstance()->setDebug();
		
		$page = $this->request['offset'] + 1;
		
		$id = isset($this->request['id']) ? $this->request['id'] : '';
		$name = isset($this->request['name']) ? $this->request['name'] : '';
		//$from = isset($this->request['from']) ? $this->request['from'] : '';
		//$to = isset($this->request['to']) ? $this->request['to'] : '';
		$is_gov = '1';
				
		$query_link = "manage.php?m=gov_price&a=scenic_price";
		$query_link .= $id ? 'id='.urlencode($id) : '';	
		$query_link .= $name ? 'name='.urlencode($name) : '';
		//$query_link .= $from ? 'from='.$from : '';
		//$query_link .= $to ? 'to='.$to : '';
		$query_link = $query_link;

		$this->request['is_gov'] = '1';
		if(!$id)
		{
			$this->request['from'] = $this->request['to'] = date('Y-m-d', get_new_date_scenic_history_price());
		}
		
		$result = get_scenic_history_price($this->request);
		
		$total_record = $result['count'];
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '10 20 50 100 200 500');
		
		$list = $result['result'];
		
		$m = $this->mod;
		$a= $this->act;
		
		include template('admin/scenic_history_price');
	}
	
	private function scenicList()
	{
		//DBFunction::getInstance()->setDebug();
		
		$page = $this->request['offset'] + 1;
		
		$name = isset($this->request['name']) ? $this->request['name'] : '';
				
		$query_link = "manage.php?m=gov_price&a=scenic_list";
		$query_link .= $name ? 'name='.urlencode($name) : '';
		$query_link = $query_link;
				
		$result = get_scenic_list($this->request);
		
		$total_record = $result['count'];
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '10 20 50 100 200 500');
		
		$list = $result['result'];
		
		include template('admin/gov_price_scenic');
	}
	
	private function scenicInfo()
	{
		$sid = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$sid)
		{
			$this->messager("请指定ID", 'manage.php?m=gov_price&a=scenic_list');
		}
		
		if(isset($this->request['c']) && ($request = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{	
			$info = get_scenic_info(array('sid'=>$sid));
			
			if(!isset($info['id']) || $info['id'] < 1)
			{
				$this->messager("景区不存在", 'manage.php?m=gov_price&a=scenic_list');
			}
		}
		
		$info['create_date'] = date('Y-m-d', $info['create_date']);

		include template('admin/gov_price_scenic_info');
	}
	
	private function addScenicPrice()
	{
		//DBFunction::getInstance()->setDebug();
		
		$sid = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$sid)
		{
			$this->messager("请指定ID", 'manage.php?m=gov_price&a=scenic_list');
		}
		
		$info = get_scenic_info(array('sid'=>$sid));
			
		if(!isset($info['id']) || $info['id'] < 1)
		{
			$this->messager("景区不存在", 'manage.php?m=gov_price&a=scenic_list');
		}
		
		$info['id'] = $sid;
		$info['min_price'] = isset($this->request['min_price']) ? (float)$this->request['min_price'] : '';
		
		$info['range'] = isset($this->request['range']) ? (float)$this->request['range'] : 0;
		$info['is_gov'] = '1';
		$info['create_date'] = isset($this->request['create_date']) ? strtotime($this->request['create_date']) : time();
		
		$result = add_scenic_history_price($info);
		
		if($result)
		{
			$this->messager("添加成功", 'manage.php?m=gov_price&a=scenic_price');
		}
		else
		{
			$this->messager("添加失败", 'manage.php?m=gov_price&a=scenic_price');
		}
	}
	
	private function deleteScenicPrice()
	{
		//DBFunction::getInstance()->setDebug();
		
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		
		$result = '';
		if(stripos($ids, ','))
		{
			$ids = explode(',', $ids);
			
			foreach($ids as $id)
			{
				$result = delete_scenic_history_price($id);
			}
		}
		else
		{
			$result = delete_scenic_history_price($ids);
		}
		
		if($result)
		{
			$this->messager("删除成功", 'manage.php?m=gov_price&a=scenic_price');
		}
		else
		{
			$this->messager("删除失败", 'manage.php?m=gov_price&a=scenic_price');
		}
	}
	
	private function hostelPrice()
	{
		//DBFunction::getInstance()->setDebug();
		
		$page = $this->request['offset'] + 1;

		$name = isset($this->request['name']) ? $this->request['name'] : '';
		$is_gov = '1';
				
		$query_link = "manage.php?m=gov_price&a=hostel_price";
		$query_link .= $name ? 'name='.urlencode($name) : '';

		$this->request['is_gov'] = '1';
		
		$result = get_hostel_history_price($this->request);
		
		$total_record = $result['count'];
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '20 50 100 200 500');
		
		$list = $result['result'];
		
		include template('admin/hostel_history_price');
	}
	
	private function hostelRoomPrice()
	{
		//DBFunction::getInstance()->setDebug();
		
		$page = $this->request['offset'] + 1;
		
		$id = isset($this->request['id']) ? $this->request['id'] : '';
		$is_gov = '1';
				
		$query_link = "manage.php?m=gov_price&a=hostel_price";
		$query_link .= $id ? 'id='.urlencode($id) : '';	
		
		$this->request['is_gov'] = '1';
		
		$result = get_hostel_room_history_price($this->request);
		
		$total_record = $result['count'];
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '20 50 100 200 500');
		
		$list = $result['result'];
		
		include template('admin/hostel_room_history_price');
	}
	
	private function hoselList()
	{
		$page = $this->request['offset'] + 1;
		
		$name = isset($this->request['name']) ? $this->request['name'] : '';
				
		$query_link = "manage.php?m=gov_price&a=hostel_list";
		$query_link .= $name ? '&name='.urlencode($name) : '';
		$query_link = $query_link;
		
		$result = get_hostel_list($this->request);
		
		$total_record = $result['count'];
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '10 20 50 100 200 500');
		
		$list = $result['result'];
		
		include template('admin/gov_price_hostel');
	}
	
	private function hostelInfo()
	{
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'manage.php?m=gov_price&a=hostel_list');
		}
		
		$info = get_hostel_info(array('hid'=>$id));
			
		if(!isset($info['id']) || $info['id'] < 1)
		{
			$this->messager("酒店不存在", 'manage.php?m=gov_price&a=hostel_list');
		}
		
		$result = get_room_types();
		$room_types = array();
		$room = array();
		foreach($result as $temp)
		{
			$room['name'] = $temp['name'];
			$room['value'] = $temp['id'];
			$room_types[] = $room;
		}
		$room_types = select('room_list', $room_types, '', 'onclick="select_room(\'room_list\')"');
		
		$room_list = get_hostel_history_price(array('id'=>$id));
				
		$info['create_date'] = date('Y-m-d');

		include template('admin/gov_price_hostel_info');
	}
	
	private function addHostelPrice()
	{
		//DBFunction::getInstance()->setDebug();
		
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'manage.php?m=gov_price&a=hostel_list');
		}
		
		$info = get_hostel_info(array('hid'=>$id));
			
		if(!isset($info['id']) || $info['id'] < 1)
		{
			$this->messager("酒店不存在", 'manage.php?m=gov_price&a=hostel_list');
		}
		
		$info['id'] = $id;
		$info['room_name'] = isset($this->request['room_name']) ? trim($this->request['room_name']) : '';
		$info['price'] = isset($this->request['price']) ? (float)$this->request['price'] : '';
		
		$info['range'] = isset($this->request['range']) ? (float)$this->request['range'] : 0;
		$info['is_gov'] = '1';
		$info['create_date'] = isset($this->request['create_date']) ? strtotime($this->request['create_date']) : time();
		
		$result = add_hostel_history_price($info);
		
		if($result)
		{
			$this->messager("添加成功", 'manage.php?m=gov_price&a=hostel_price');
		}
		else
		{
			$this->messager("添加失败", 'manage.php?m=gov_price&a=hostel_price');
		}
	}
	
	private function deleteHostelPrice()
	{
		//DBFunction::getInstance()->setDebug();
		
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		if(!$ids)
		{
			$this->messager("请指定ID", 'manage.php?m=gov_price&a=hostel_price');
		}
		
		$result = '';
		if(stripos($ids, ','))
		{
			$ids = explode(',', $ids);
			
			foreach($ids as $id)
			{
				$result = delete_hostel_history_price($id);
			}
		}
		else
		{
			$result = delete_hostel_history_price($ids);
		}
		
		if($result)
		{
			$this->messager("删除成功", 'manage.php?m=gov_price&a=hostel_price');
		}
		else
		{
			$this->messager("删除失败", 'manage.php?m=gov_price&a=hostel_price');
		}
	}
}